package org.millioncall.yueyoga.admin.model;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.millioncall.yueyoga.admin.model.base.BaseUser;

public class User extends BaseUser implements java.io.Serializable {
	private static final long serialVersionUID = -7055699862330325940L;
	private String headImgFilePath;

	public User() {
	}

	public User(String username, String password, String email) {
		super(username, password, email);
	}

	// 取得字符串连接的多个用户组
	public String getStrGroups() {
		List<Group> groups = this.getGroups();
		if (groups.isEmpty()) {
			return null;
		} else {
			String str = "";
			for (Group group : groups) {
				str += group.getName() + ",";
			}
			str = str.substring(0, str.length() - 1);
			return str;
		}
	}

	// 用户名是否到期
	public boolean isUsernameExpired() {
		Date expireTime = this.getExpireTime();
		if (expireTime == null) {
			return false;
		}
		return isExpired(expireTime);
	}

	// 权限是否到期
	public boolean isAuthorityExpired() {
		Date expireTime = this.getAuthorityExpireTime();
		if (expireTime == null) {
			return false;
		}
		return isExpired(expireTime);
	}

	// 返回未解密的文件名
	public String getHeadImgFileName() {
		DbFile file = getHeadImgFile();
		return file == null ? null : file.getFileName();
	}

	public void setHeadImgFilePath(String path) {
		this.headImgFilePath = path;
	}

	// 返回头像路径
	public String getHeadImgFilePath() {
		return this.headImgFilePath;
	}

	// 判断一个日期是否过期
	private boolean isExpired(Date target) {
		if (target == null) {
			return false;// 永不过期
		}
		Date now = Calendar.getInstance().getTime();// 取得当前时间
		return target.before(now);
	}
}